/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "Entrez.h"

#include <QtCore/QCoreApplication>
#include <QtCore/QFile>
#include <QtCore/QUrlQuery>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>

#include "Parser.h"

using namespace Bio;

QNetworkAccessManager *Entrez::s_network = NULL;
const QString	c_toolName = "BioQt";
const QString	c_email = "jaromschow@msn.com";

///////////////////////////////////////////////////////////////////////////////
//	Entrez
///////////////////////////////////////////////////////////////////////////////

/**
	@class Entrez
	@brief Base class for Entrez operations.

	Base class for objects that implement communication with the NCBI Entrez
	functions.

	See http://www.ncbi.nlm.nih.gov/books/NBK25499/ for details on
	the Entrez functions.
**/

/**
	Default Constructor.
**/
Entrez::Entrez()
{
}

/**
**/
Entrez::~Entrez()
{
}

/**
**/
void Entrez::error( const QString &msg )
{
}

/**
**/
void Entrez::warning( const QString &msg )
{
}

/**
**/
bool Entrez::execPost( const QUrl &url, const QStringList &ids, Parser &parser )
{
	QUrl		urlCopy( url );
	QUrlQuery	urlQuery;

	if( !s_network )
		s_network = new QNetworkAccessManager;

	urlQuery.addQueryItem( "tool", c_toolName );
	urlQuery.addQueryItem( "email", c_email );
	urlCopy.setQuery( urlQuery );

	QNetworkRequest	req( urlCopy );
	QNetworkReply	*reply = s_network->get( req );
	bool			status = true;
	
	while( !reply->isFinished() )
		qApp->processEvents();

	QNetworkReply::NetworkError	err = reply->error();

	if( err == QNetworkReply::NoError ){
		status = parser.parse( *reply );
	}
	else {
		status = false;
		error( reply->errorString() );
	}

	reply->deleteLater();

	return status;
}

/**
**/
bool Entrez::execGet( const QUrl &url, Parser &parser )
{
	QUrl		urlCopy( url );
	QUrlQuery	urlQuery( url );

	if( !s_network )
		s_network = new QNetworkAccessManager;

	urlQuery.addQueryItem( "tool", c_toolName );
	urlQuery.addQueryItem( "email", c_email );
	urlCopy.setQuery( urlQuery );

	QNetworkRequest	req( urlCopy );
	QNetworkReply	*reply = s_network->get( req );
	bool			status = true;
	
	while( !reply->isFinished() )
		qApp->processEvents();

	QNetworkReply::NetworkError	err = reply->error();

	if( err == QNetworkReply::NoError ){
		status = parser.parse( *reply );
	}
	else {
		status = false;
		error( reply->errorString() );
	}

	reply->deleteLater();

	return status;
}
